Don't raise UnusedElementError when used has more elems than Grammar #24
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue
Currently,
create_grammar
will check if all named Elements are used by comparing theused
variable (which tracks the named elements found by_used_checker
) withelems
(which tracks the values assigned to the grammar). The comparison is done with an!=
operator, which will fail for states where there are more used elements than included element.Why make this change?
This change would allow users to be assign Element names outside the grammar initialization.
I noticed this while trying to add a name to a Regex element. Normally, names are added by using a class variable assignment in
Grammar
, however I have a grammar that conditionally includes Regex Elements based on user input. There are many different Regexes that could be included in the grammar, so naming them makes theResult.expecting
easier to understand.Steps to reproduce
This gives us the following state:
This fails the condition
if used != elems:
and raises the following exception:Conclusion
When
int_value
/float_value
are included in the grammar, they are inused
, but it is not inelems
, so when initializing the grammar, this raises anUnusedElementError
. I felt like the correct approach was to update the logic to only raise anUnusedElementError
when there are Elements inelems
that are not inused
.Let me know if there are other perspectives I should be considering. I'm not sure if it's intended for names to be manually set on Elements, but the only issue I saw was this condition, so I wanted to know if we should change it.
Great job on pyleri, btw. I love it.